/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.permit.mapper;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import net.siufung.market.base.permit.controller.model.dto.RolePermitCountDto;
import net.siufung.market.base.permit.mapper.entity.RolePermit;
import net.siufung.mybatis.base.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 系统角色权限表 Mapper 接口
 * </p>
 *
 * @author 陈建峰
 * @since 2022-07-05 16:14:50
 */
@Mapper
public interface IRolePermitMapper extends BaseMapper<RolePermit> {

    /**
     * 获取角色使用权限数量列表
     * @param roleIds 角色Id集合
     * @return List<RolePermitCountDto>
     */
    default List<RolePermitCountDto> getRolePermitCountList(Integer[] roleIds) {
        List<RolePermitCountDto> rolePermitCountDtoList = Lists.newArrayList();
        RolePermitCountDto rolePermitCountDto;
        if(ArrayUtil.isNotEmpty(roleIds)){
            List<RolePermit> rolePermitList = this.selectList(new LambdaQueryWrapper<RolePermit>()
                    .in(RolePermit::getRoleId, Arrays.asList(roleIds)));
            for (Integer roleId: roleIds) {
                rolePermitCountDto = new RolePermitCountDto();
                rolePermitCountDto.setId(roleId);
                int count = 0;
                for (RolePermit rolePermit: rolePermitList) {
                    if(roleId.intValue() == rolePermit.getRoleId().intValue()){
                        count ++;
                    }
                }
                rolePermitCountDto.setPermitCount(count);
                rolePermitCountDtoList.add(rolePermitCountDto);
            }
        }
        return rolePermitCountDtoList;
    }

}
